#ifndef MSGQUEUE_H
#define MSGQUEUE_H

#include "global.h"
#include "ByteString.h"

namespace ZY {

    enum  MsgQueueType{
        MAIN_QUEUE = 0 ,
        SWAP_CACHE_QUEUE = 1 ,
        DB_QUEUE  = 2,
        LOGIN_MSG_QUEUE = 3 ,
        AGENT_FREE_QUEUE = 4 ,
        LOGIN_KICKOFF_QUEUE = 5,
        YB_CHANGE_QUEUE = 6,
        BLOCK_QUEUE = 7,
        BLOCK_MSG_QUEUE = 8
    } ;

    class MsgQueue
    {
        public:
            MsgQueue();
            virtual ~MsgQueue();

            typedef   void (*CallbackFn)(ByteString&) ;

            static MsgQueue &getInstance( string instanceName );
            static MsgQueue &getInstance( int instanceId );

            static void destroyInstance( string instanceName );
            static void destroyInstance( int instanceId );

            void add( MsgQueue::CallbackFn fn );
            void add( MsgQueue::CallbackFn fn , ByteString & ext_data );

            bool run_front();

            void run_all() {while( run_front() );}

            void swap( MsgQueue & m );

            bool merginTo( MsgQueue & m );

            bool blockMerginTo(MsgQueue&);

            bool merginFrom( MsgQueue & m );

        protected:
        private:

            struct sMsgQueueData {
                MsgQueue::CallbackFn fn;
                ByteString ext;
            };



            static map<string, MsgQueue* > _s_instances;
            static map<int, MsgQueue* > _i_instances;

            list< sMsgQueueData * > _queue;

            pthread_mutex_t lock ;
    };

}

#endif // MSGQUEUE_H
